<!--
* @Author: bln
* @Date:  2023-11-09 14:12:03
 * @FilePath: \zmglpt-ui-mobile-v1.1\dev\pages\check\ManualDeductionInitiated\components\addManualDeductionApply.vue
* @Description: 市管-售户信用违规行为维护审批--福建 统一待办审批跳转页
-->

<template>
  <view class="page">
    <!-- #ifndef H5-DINGDING -->
    <theme-navbar :is-white="false" :title="currentTitle" :custom-back="goBack" style="z-index: 99999"></theme-navbar>
    <!-- #endif -->
    <view class="content">
      <u-form ref="uForm" :rules="rules" :model="baseInfo" label-position="top" label-width="auto">
        <form-section title="基础信息"></form-section>
        <u-form-item label="维护人">
          <u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.personName"></u-input>
        </u-form-item>
        <u-form-item label="维护时间">
          <u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.createTime"></u-input>
        </u-form-item>
        <u-form-item label="许可证号" :required="true"  prop="licNo">
          <u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.licNo"></u-input>
          <u-button type="primary" size="mini" @click="handleGoToRetList" :disabled="isDisabled">
            选择
          </u-button>
        </u-form-item>
        <u-form-item label="负责人">
          <u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.managerName"></u-input>
        </u-form-item>
        <u-form-item label="企业（字号）名称">
          <u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.companyName"></u-input>
        </u-form-item>
        <u-form-item label="统一社会信用代码">
          <u-input placeHolder="" type="text" :disabled="true" v-model="baseInfo.businessLicNo"></u-input>
        </u-form-item>
        <u-form-item label="违法违规日期" prop="manualDate" :required="true">
          <u-input :disabled="true" input-align="left" placeholder="请选择日期" v-model="baseInfo.manualDate" @click="()=>{!isDisabled &&goShowRegDate()}" />
        </u-form-item>
        <u-gap :marginRight="getGapMarginRowNumber()" :marginLeft="getGapMarginRowNumber()" :height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
        
        <form-section title="违法违规类别选择"></form-section>
        <scroll-view :scroll-y="false" :scroll-x="true" :style="{ height: scrollRealHeight, marginTop: '5px', paddingBottom:'10px'}">
          <u-table class="tb-lay" ref="tbRef">
            <u-tr>
              <u-th width="6%">序号</u-th>
              <u-th width="8%"></u-th>
              <u-th width="12%">行为编码</u-th>
              <u-th width="28%">涉烟违法违规行为</u-th>
              <u-th width="10%">标准名称</u-th>
              <u-th width="13%">扣分值</u-th>
              <u-th width="23%">法律依据</u-th>
            </u-tr>
            <u-tr v-for="(it, index) in dataSource" :key="'ds' + index">
              <u-td width="6%">{{ index + 1 }}</u-td>
              <u-td width="8%" class="u-th" style="padding: 7px 0px">
                <view class="radios">
                  <checkbox style="transform:scale(0.8)"  :disabled="isDisabled" :checked="it.checked" @click="!isDisabled && handlerSelect(it)" />
                </view>
              </u-td>
              <u-td width="12%">{{ it.targetCode || '&nbsp;' }}</u-td>
              <u-td width="28%">{{ it.targetName || '&nbsp;' }}</u-td>
              <u-td width="10%">{{ it.standardName || '&nbsp;' }}</u-td>
              <u-td width="13%">{{ it.creditScore || '&nbsp;' }}</u-td>
              <u-td width="23%">{{ it.lawBasis || '&nbsp;' }}</u-td>
            </u-tr>
          </u-table>
        </scroll-view>
        <u-form-item label="备注">
          <u-input placeHolder="" type="textarea" :maxlength="100" :disabled="isDisabled" v-model="baseInfo.remark"></u-input>
        </u-form-item>
        <!-- 审批历史开始 -->
        <view v-if="AddOrEdit=='view'">
          <u-gap :marginRight="getGapMarginRowNumber()" :marginLeft="getGapMarginRowNumber()" :height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
          <form-section title="审批历史"></form-section>
          <u-form-item label="" class="u-m-l-12">
            <flow-his-no-doc :bizUuid="prvParam.bizUuid" :processDefKey="prvParam.gfCode" :endTitle="endTitle"></flow-his-no-doc>
          </u-form-item>
        </view>
        <!-- 审批历史结束 -->
      </u-form>
    </view>
    <view class="submit-btns btn-other-cls" v-if="!isDisabled">
      <u-button class="u-btn" type="primary" @click="handleSave('1')">保存</u-button>
      <u-button class="u-btn" type="primary" @click="handleSave('2')">提交</u-button>
    </view>
    <u-calendar mode="date" :max-date="yestDate" v-model="isShowRegDate" @change="changeDate('undertakeDate',$event)">
    </u-calendar>
  </view>
</template>

<script>
import DicChoice from "@/components/dic_choice.vue";
import formSection from "@/components/form-section.vue";
import FlowHisNoDoc from "@/components/flow-his-no-doc.vue";
import creditService from "@/service/check/check.service.js";
import { dateUtils } from "@/common/util";
import { M_DAY_PLAN_STATUS } from "@/service/check/mapping/enum.js";

export default {
  components: {
    formSection,
    FlowHisNoDoc,
    DicChoice
  },
  data() {
    return {
      currentTitle:'涉烟违法违规行为发起',
      yestDate: this.$u.timeFormat(Date.now()),
      isShowRegDate: false,
      step: "",
      endTitle: "",
      ifSub: false,
      approveDis: true,
      approvalDisabled: false,
      departmentIsEdit: true,
      responsibleIsEdit: true,
      prvParam: {
        gfCode :"licIllegal"
      }, // 路由参数
      checkedBoxList:[],
      baseInfo: {
        retailerUuid: "",
        manualDate: "",
        remark: "",
        licNo: "",
        personName: this.$storage.get(this.$rp.common.USER).user.personName,
        createTime: this.$u.timeFormat(Date.now()),
        manualStatus: "",
        companyName: "",
        businessLicNo: "",
        managerName: "",
        targetName: "",
        creditLevel: "",
        repairAfterCreditLevel: "",
        creditTargetHandList: [],
        problemDescription: "",
        handlePersonUuids: this.$storage.get(this.$rp.common.USER).user.personUuid,
        handlePerson: this.$storage.get(this.$rp.common.USER).user.personName,
        targetCode: "",
        standardUuid: "",
        manualRecordUuid: ""
      }, // 详情
      tableOriList: [], // 违法违规类别all
      scrollRealHeight: "50px",
      dictList: [
        {
          label: "依申请修复",
          value: "02"
        },
        {
          label: "异议修复",
          value: "03"
        },
        {
          label: "特殊修复",
          value: "04"
        }
      ],
      departmentParams: {
        approveMemoResult: "01",
        approveMemo: "同意",
        handlePerson: " ",
        applyDate: " "
      },
      responsibleParams: {
        approveMemoResult: "01",
        approveMemo: "准予通过",
        handlePerson: " ",
        applyDate: " "
      },
      AddOrEdit: "",
      isDisabled: false,
      dataSource: [],
      rules: {
        licNo: [
          {
            required: true,
            message: "请选择许可证号",
            trigger: ["change", "blur"]
          }
        ],
        manualDate: [
          {
            required: true,
            message: "请选择违法违规日期！",
            trigger: ["change", "blur"]
          }
        ]
      }
    };
  },
  // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  onLoad(options) {
    this.AddOrEdit = options.AddOrEdit;
    if (options.manualRecordUuid) {
      this.baseInfo.manualRecordUuid = options.manualRecordUuid;
      this.prvParam.bizUuid = options.manualRecordUuid;
      this.getLicIllegalRecord(options.manualRecordUuid);
      
    }else{
      this.getLicCreditHandTargetList();
    }
    if (this.AddOrEdit == "view") {
      this.isDisabled = true;
      this.currentTitle='涉烟违法违规行为查看'
    } else if(this.AddOrEdit == "edit") {
      this.isDisabled = false;
      this.currentTitle='涉烟违法违规行为编辑'
    }else{
      this.isDisabled = false;
      this.currentTitle='涉烟违法违规行为发起'
    }
    
  },
  computed: {
  },
  methods: {
    handleRelease() {
      this.toSelectPerson();
    },
    changeHeight() {
      this.$nextTick(() => {
        this.$refs.cons.init();
        this.$refs.files.init();
      });
    },
    getGapMarginRowNumber() {
      return "-" + this.$theme.cm_content_paddng_row.replace("rpx", "");
    },
    // 选择审批人
    toSelectPerson(docInsUuid = "") {
      this.$u.route({
        url: "/pages/commonPage/flowUserSelectComponent",
        params: {
          flowFormCode: this.prvParam.gfCode,
          docInsUuid: docInsUuid
        }
      });
    },
    getLicIllegalRecord(manualRecordUuid){
      this.$modal.showLoading('加载中...')
      creditService
        .getLicIllegalManagement({ manualRecordUuid: manualRecordUuid })
        .then(res => {
          if (res.success) {
            Object.keys(this.baseInfo).forEach(key => {
              this.baseInfo[key] =
                res.bean[key] == undefined ? this.baseInfo[key] : res.bean[key];
            });
            this.checkedBoxList=res.bean.checked
            this.getLicCreditHandTargetList();
           this.endTitle = res.bean.status == M_DAY_PLAN_STATUS.TERMINATION ? '结束' : '...'
          this.$modal.hideLoading()
          } else {
            this.$modal.hideLoading()
            this.$u.error(res.msg);
          }
        });
    },
    //保存
    handleSave(type) {
      this.$refs.uForm.validate(valid => {
        if (!valid) return;
        this.baseInfo.creditTargetHandList = this.dataSource.filter(
          item => item.checked
        );
        if (type == "1") {
          this.baseInfo.manualStatus = "00"; //草稿00，提交01
          creditService.saveLicIllegalManageRecord(this.baseInfo).then(res => {
            if (res.success) {
              this.backPrev({
                paramKey: "SaveRefresh",
                param: true
              });
            } else {
              this.$u.error(res.msg);
            }
          });
        } else {
          this.baseInfo.manualStatus = "01";
          this.toSelectPerson();
          console.log("提交");
        }
      });
    },
    handleGoToRetList() {
      this.$u.route(
        "pages/check/ManualDeductionInitiated/components/retailerList"
      );
    },
    error() {
      uni.navigateBack();
    },
    changeDate(type, e) {
      this.baseInfo.manualDate = dateUtils.dateFormat(
        "yyyy-MM-dd",
        new Date(e.result)
      );
    },
    getLicCreditHandTargetList() {
      creditService
        .queryLicCreditHandTargetList({
          page: 1,
          limit: 30,
          // retailerUuid: this.baseInfo.retailerUuid
        })
        .then(res => {
          if (res.success) {
            this.dataSource = res.data;
            setTimeout(() => {
              this.scrollRealHeight =
                this.$refs.tbRef.$el.clientHeight + 5 + "px";
            }, 100);
            if (this.dataSource.length) {
              this.dataSource.forEach(element => {
                  if(this.AddOrEdit=='add'){
                    element.checked = false;
                  }else{
                      if(this.checkedBoxList.length){
                        this.checkedBoxList.forEach(item=>{
                          if(element.standardUuid==item){
                            element.checked=true
                          }
                        })
                      }
                  }
                
              });
            }
          }
          console.log(res);
        });
    },
    handlerSelect(item) {
      this.dataSource.forEach((element, index) => {
        if (item.standardUuid == element.standardUuid) {
          console.log(element.checked);
          if (element.checked) {
            console.log(element);
            element.checked = false;
          } else {
            console.log(element);
            element.checked = true;
          }
        }
      });
      this.$set(this.dataSource);
      console.log(this.dataSource);
    },
    goShowRegDate(){
      this.isShowRegDate = true
    },
    // 根据页面返回的参数处理相应逻辑, 方法名updateBackState固定
    updateBackState(param) {
      // 选中的卷烟
      console.log("选中的数据", param);
      console.log(param);
      if (param.flowUserSelectBack) {
        // 选择审批人
        let nextPersons = [];
        for (let fu of param.flowUserSelectBack) {
          nextPersons.push(fu.personId);
        }
        this.baseInfo.nextPersonUuids = nextPersons.join(",");
        creditService.saveLicIllegalManageRecord(this.baseInfo).then(res => {
          if (res.success) {
            this.backPrev({
              paramKey: "SubmitRefresh",
              param: true
            });
          } else {
            this.$u.error(res.msg);
          }
        });
      }
      let paramRetailer = param[this.$rp.check.CHECK_CLIENT][0];
      if (paramRetailer.retailerUuid) {
        this.baseInfo.licNo = paramRetailer.licNo;
        // this.baseInfo.applicant = paramRetailer.managerName;
        // this.baseInfo.personName = paramRetailer.managerName;
        this.baseInfo.companyName = paramRetailer.companyName;
        this.baseInfo.businessLicNo = paramRetailer.businessLicNo;
        this.baseInfo.managerName = paramRetailer.managerName;
        // this.baseInfo.targetName=paramRetailer.targetName
        // this.baseInfo.repairScore=paramRetailer.repairScore
        this.baseInfo.repairBeforeCreditLevel = paramRetailer.creditLevel;
        this.baseInfo.creditLevel = paramRetailer.creditLevel;
        this.baseInfo.repairBeforeCreditScore = paramRetailer.creditScore;
        this.baseInfo.retailerUuid = paramRetailer.retailerUuid;
        this.getLicCreditHandTargetList();
      }
    }
  }
};
</script>

<style scoped lang="scss">
.page {
  padding: 0;
}
.title {
  width: 8rpx;
  height: 32rpx;
  background: #0079f2;
  opacity: 1;
  margin-left: -5px;
  border-radius: 4rpx;
  float: left;
  margin-right: 16rpx;
}
.basicHead {
  height: 36rpx;
  line-height: 36rpx;
  text-align: center;
  float: left;
  font-weight: bold;
  font-size: 16px;
}
.sub-title {
  background: #f5f5f5;
  color: #989ea1;
  font-size: 24rpx;
  border-radius: 4px;
  margin-right: 10px;
  padding: 4px 8px;
}
.f-ctn {
  width: 100%;
}
.tb-lay {
  width: 900px;
  margin-left: 1px;
  margin-right: 1px;
}
.content {
  padding-left: $cm-content-paddng-row;
  padding-right: $cm-content-paddng-row;
  padding-top: $cm-conent-padding-column;
  padding-bottom: 70px;
}
.td-text {
  word-break: break-all;
  word-wrap: break-word;
}
/deep/.u-td {
  height: auto;
}
</style>
